package com.jlt.dao.account;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter.MatchType;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.google.common.collect.Lists;
import com.jlt.entity.account.Dept;

/**
 * 部门对象的泛型DAO.
 * 
 * @author zhoutao
 */
@Component
public class DeptDao extends HibernateDao<Dept, Long> {

	/**
	 * 根据部门名与交部门搜索部门
	 */
	public Page<Dept> searchDept(Page<Dept> page, String name, Long parentDeptId) {
		List<Criterion> cList = new ArrayList<Criterion>();
		if (StringUtils.isNotBlank(name))
			cList.add(buildCriterion("name", name, MatchType.LIKE));

		cList.add(buildCriterion("parentDept.id", parentDeptId, MatchType.EQ));
		cList.add(buildCriterion("status", "A", MatchType.EQ));
		return findPage(page, cList.toArray(new Criterion[cList.size()]));
	}

	/**
	 * 根据parentId获取部门
	 */
	public List<Dept> getChildDepts(Long parentId) {
		List<Criterion> cList = Lists.newArrayList();
		cList.add(buildCriterion("parentDept.id", parentId, MatchType.EQ));
		cList.add(buildCriterion("status", "A", MatchType.EQ));
		return find(cList.toArray(new Criterion[cList.size()]));
	}
}
